<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">

      <el-form-item label="所属行业" prop="industryId">
        <div class="w-[200px]">
          <el-select class="flex-1" v-model="queryParams.industryId" placeholder="请选择所属行业" clearable>
            <el-option class="flex-1" v-for="item in industryList" :key="item.industryId" :label="item.name"
              :value="item.industryId" />
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="所属系统" prop="systemName">
        <el-input style="width: 200px;" v-model="queryParams.systemName" placeholder="请输入所属系统" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品标题" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" width="55">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="name" />

      <el-table-column label="商品图片" width="100" align="center" prop="image">
        <template #default="scope">
          <el-image style="width: 50px; height: 50px;" :src="scope.row.image"
            :preview-src-list="[scope.row.image]" preview-teleported>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="所属行业" align="center" prop="industryName"  />
      <el-table-column label="所属系统" align="center" prop="systemName" show-overflow-tooltip />


      
      <el-table-column label="质保期限" align="center" prop="guarantee" />
      <el-table-column label="是否支持7天无理由退换 " align="center" prop="isNoReasonReturn">
        <template #default="scope">
          <div>
            <span v-if="scope.row.isNoReasonReturn">是</span>
            <span v-if="!scope.row.isNoReasonReturn">否</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商家名称" align="center" prop="merchantName" />

    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品图片" prop="imageList">
              <el-input v-model="form.im" @change="((e) => { form.imageList[0] = e })" placeholder="请输入图片" />
              <!-- <el-image :src="form.imageList" /> -->
              <!-- <imageUpload /> -->
              <!-- <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
                <el-button>
                  选择
                  <el-icon class="el-icon--right">
                    <Upload />
                  </el-icon>
                </el-button>
              </el-upload> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产厂家" prop="produceMerchantName">
              <el-input v-model="form.produceMerchantName" placeholder="请输入生产厂家" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摆放位置" prop="place">
              <el-input v-model="form.place" placeholder="请输入摆放位置" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品标准" prop="produceProcessName">
              <el-input v-model="form.produceProcessName" placeholder="请输入产品标准" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注意事项" prop="attention">
              <el-input v-model="form.attention" placeholder="请输入注意事项" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="规格" prop="spec">
              <el-input v-model="form.spec" placeholder="请输入规格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入型号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="库存" prop="stock">
              <el-input v-model="form.stock" type="number" placeholder="请输入库存" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sortNum">
              <el-input v-model="form.sortNum" type="number" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="剂量" prop="dosage">
              <el-input v-model="form.dosage" placeholder="请输入剂量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="损耗" prop="ullage">
              <el-input v-model="form.ullage" placeholder="请输入损耗" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="存储方式" prop="storageMethod">
              <el-input v-model="form.storageMethod" placeholder="请输入存储方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
            </el-form-item>
          </el-col>
        </el-row>



        <el-row>
          <el-col :span="12">
            <el-form-item label="合格证书" prop="qualityCert">
              <el-input v-model="form.qualityCert" placeholder="请输入合格证书" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验证书" prop="checkCert">
              <el-input v-model="form.checkCert" placeholder="请输入检验证书" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品码" prop="code">
              <el-input v-model="form.code" placeholder="请输入商品码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品码图片" prop="codeImage">
              <el-input v-model="form.codeImage" placeholder="请输入商品码图片" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主要成分" prop="component">
              <el-input v-model="form.component" placeholder="请输入主要成分" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主要功效" prop="effect">
              <el-input v-model="form.effect" placeholder="请输入主要功效" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单一疗程" prop="treatPeriod">
              <el-input v-model="form.treatPeriod" placeholder="请输入单一疗程" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用方法" prop="usage">
              <el-input v-model="form.usage" placeholder="请输入使用方法" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保质期/天" prop="expireDays">
              <el-input v-model="form.expireDays" type="number" placeholder="请输入保质期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格" prop="price">
              <el-input v-model="form.price" placeholder="请输入价格" />
            </el-form-item>
          </el-col>
        </el-row>
        <!--  -->
        <!--  -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="代理价" prop="agentPrice">
              <el-input v-model="form.agentPrice" placeholder="请输入商品代理价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批发价" prop="batchPrice">
              <el-input v-model="form.batchPrice" placeholder="请输入商品批发价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采购价" prop="purchasePrice">
              <el-input v-model="form.purchasePrice" placeholder="请输入采购价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进货价" prop="replenishPrice">
              <el-input v-model="form.replenishPrice" placeholder="请输入进货价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="惠城价" prop="huiPrice">
              <el-input v-model="form.huiPrice" placeholder="请输入惠城价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员价" prop="vipPrice">
              <el-input v-model="form.vipPrice" placeholder="请输入会员价" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--  -->
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Notice">
import {
  authApiIndustryList
} from "@/api/login.js";

const industryList = ref([])
function authApiIndustryListApi() {
  authApiIndustryList().then(
    (res) => {
      industryList.value = [...res];
    }
  );
}

onMounted(() => {
  authApiIndustryListApi()
})


import {
  listGuarantee,
  productDelete,
  productUpdate,
  productDetail,
  productAdd,
} from "@/api/backProduct/backProduct";
const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type, scope_status } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type",
  "scope_status"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

let im = ref("")

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    name: undefined,
  },
  rules: {
    name: [
      { required: true, message: "商品名称不能为空", trigger: "blur" },
    ],
    price: [
      { required: true, message: "商品价格不能为空", trigger: "blur" },
    ],
    stock: [
      { required: true, message: "商品库存不能为空", trigger: "blur" },
    ],
    imageList: [
      { required: true, message: "图片不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品列表 */
function getList() {
  loading.value = true;
  listGuarantee(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    "agentPrice": 0,
    "attention": "",
    "batchPrice": 0,
    "checkCert": "",
    "code": "",
    "codeImage": "",
    "component": "",
    "dosage": "",
    "effect": "",
    "expireDays": 0,
    "huiPrice": 0,
    "imageList": [],
    "materialCode": "",
    "model": "",
    "name": "",
    "place": "",
    "price": 0,
    "produceMerchantName": "",
    "produceProcessName": "",
    "productCategoryTypeId": 1763848515436650498,
    "purchasePrice": 0,
    "qualityCert": "",
    "replenishPrice": 0,
    "sortNum": "",
    "spec": "",
    "stock": "",
    "storageMethod": "",
    "treatPeriod": "",
    "ullage": "",
    "usage": "",
    "vipPrice": 0
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.productId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商品";
  form.productCategoryTypeId = 1763848515436650498
  return;
  productDetail().then((response) => {
    console.log(response, "response");
    form.value = { ...form.value, ...response };

  });

}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const productId = row.productId || ids.value;
  productDetail(productId).then((response) => {
    form.value = { ...form.value, ...response };
    open.value = true;
    title.value = "修改商品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.productId != undefined) {
        productUpdate(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        productAdd(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const productId = row.productId || ids.value;
  proxy.$modal
    .confirm('是否确认删除商品标题为"' + productId + '"的数据项？')
    .then(function () {
      return productDelete(productId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

getList();
</script>


<style scoped>
.el-image {
  width: 200px;
  height: 60px;
}
</style>